<?php

namespace app\admin\controller\property;

use app\common\controller\Backend;
use think\Db;

/**
 * 办公物品库存
 *
 * @icon fa fa-circle-o
 */
class Repertory extends Backend
{

    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    protected $relationSearch = true;

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Repertory;
    }

    /**
     * 查看
     */
    public function index()
    {

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            $a = json_decode(request()->param()['filter'],true);
            $b = json_decode(request()->param()['op'],true);
            $filter = '';
            $op = '';

            if (isset($a['count'])){
                if ($a['count'] == 1){
                    $orders['count'] = 'desc';
                }else{
                    $orders['count'] = 'asc';
                }
                unset($a['count']);
                unset($b['count']);


               $filter = json_encode($a);
               $op = json_encode($b);

            }else{
                $orders['id'] = 'desc';
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams($filter,$op);
            //list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with([
                    'vindicate' => function ($query) {
                        $query->withField('id,name,specification');
                    }
                ])
                ->where($where)
                ->order($orders)
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }

        return $this->view->fetch();
    }


}
